#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
#include <QMessageBox>
#include "registerwindow.h"
#include "funwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("智能系统");
    ui->insureSlider->setMaximum(100);
    ui->insureSlider->setMinimum(0);
    ui->insureSlider->hide();
    ui->myWidget->hide();

    //给QLineEdit控件安装事件过滤器
    //QObject::installEventFilter()允许对象拦截指向另一个对象的事件。
    ui->IdlineEdit->installEventFilter(this);
    ui->passwordlineEdit->installEventFilter(this);

    //连接数据库
    db = QSqlDatabase::addDatabase("QSQLITE");

    db.setDatabaseName("user.db");

    if(!db.open())
    {
        qDebug() << "打开数据库失败";
    }

    //创建表
    QSqlQuery query;

    QString sqlTable = "create table if not exists staff(id integer primary key autoincrement,"
                       "name text,"
                       "password text);";

    if(!query.exec(sqlTable))
    {
        qDebug() << "创建表失败";
    }

}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if ( (watched == ui->IdlineEdit) && (event->type() == QEvent::MouseButtonPress) )
    {
        callKeyBoard();
        ui->IdlineEdit->setFocus();
    }
    else if ( (watched == ui->passwordlineEdit) && (event->type() == QEvent::MouseButtonPress) )
    {
        callKeyBoard();
        ui->passwordlineEdit->setFocus();
    }

    return QMainWindow::eventFilter(watched,event);
}

void MainWindow::callKeyBoard()
{
    ui->myWidget->show();
}


void MainWindow::on_loginButton_clicked()
{

    QSqlQuery query;

    QString sqlSelect = "select * from staff;";
    if(!query.exec(sqlSelect))
    {
        qDebug() << "查询失败";
    }


    while (query.next())
    {
        QString name = query.value("name").toString();
        QString password = query.value("password").toString();

        if(name == ui->IdlineEdit->text() && password == ui->passwordlineEdit->text())
        {
            QMessageBox::information(this,"验证信息","请拖动滑块至最右侧进行验证。");
            ui->insureSlider->show();
            break;
        }

        else if(ui->IdlineEdit->text() == "" || ui->passwordlineEdit->text() == "")
        {
            QMessageBox::information(this,"登录信息","请规范输入信息。");
            break;
        }

        else
        {
            QMessageBox::information(this,"登录信息","查无此账号。");
        }
    }
}

void MainWindow::on_create_usrButton_clicked()
{
    RegisterWindow *rw = new RegisterWindow;
    rw->show();
    this->hide();
}


void MainWindow::on_checkBox_stateChanged(int arg1)
{
    if(arg1 == 2)
    {
        ui->passwordlineEdit->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->passwordlineEdit->setEchoMode(QLineEdit::Password);
    }
}

void MainWindow::on_insureSlider_valueChanged(int value)
{

    if(value == 100)
    {
        QMessageBox::information(this,"登录提示","登录成功!");
        funWindow *fw = new funWindow;
        fw->show();
        this->hide();
    }
}

void MainWindow::on_minButton_clicked()
{
    ui->myWidget->hide();
}
